﻿ <%@ page contentType="text/html; charset=UTF-8" %>

<table id="rolepermission_grid" width="100%">
<tr>
    <td width="45%" class="border4"><div id="rolepermission_left_div" style="overflow:auto;"><div class="ui-title re_ui-corner-top re_ui-helper-clearfix">权限类型</div><ul id="rolepermission_left_tree" class="tree" style="overflow:auto;"></ul><div></td>
    <td width="50%" class="border4"><div id="rolepermission_right_div" style="overflow:auto;"><div class="ui-title re_ui-corner-top re_ui-helper-clearfix"><div style='float:left;height=20px;'>角色权限</div><div style='float:right;height=20px;margin-right:10px;'><a href='javascript:void(0)' onclick='saveRolepermission()' class='save'>保存</a></div></div><ul id="rolepermission_right_tree" class="tree" style="overflow:auto;"></ul><div></td>
 </tr>
</table>     
<script> 

    var rolepermission={};
    
    rolepermission.roleid=0;
   // rolepermission.zleftNodes  =[{ name:"菜单", id:"0",checked:true},{ name:"行为", id:"1"},{ name:"元素", id:"2"},{ name:"其它", id:"3"}];

	rolepermission.leftsetting = {
		checkable: true,
		checkStyle: "radio",
		checkRadioType: "all",
		callback: {
			change:	zrlefTreeOnChange
		}
	};

	function zrlefTreeOnChange(event, treeId, treeNode) {
		rolepermission.roleid=treeNode.id;
		rightTree('system/RolePermission/getRolePermissionTree.do?sort=0'+'&roleId='+rolepermission.roleid);
	}

	function rlefTree() {
		rolepermission.zleftTree = $("#rolepermission_left_tree").zTree(rolepermission.leftsetting, rolepermission.zleftNodes);
	}
	
	function saveRolepermission(){
	    var nodes =rolepermission.zrightTree.getCheckedNodes();
	    var length=nodes.length;
	    var permissionIdarr ='';
	    for(var i=0;i<length;i++){
	        if(i > 0){
                 permissionIdarr += ', ';
            }
            permissionIdarr += nodes[i].id;
	    }
	    if(permissionIdarr.length ==0){
	        alertmsg("请选择角色权限后再保存!","alert");
	        return;
	    }
	    
	    $.ajax({
            type: "POST",
            url:"system/RolePermission/deleterolePermission.do",
            dataType: "json",
            data:{
                 permissionIdarr:permissionIdarr,
				 roleId:rolepermission.roleid,
			     sort:0
            },
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            success:function(json){
                 if(json.success){
                      alertmsg(json.msg,"info");
                 }else{
                      alertmsg(json.msg,"alert");
                 }
            }
        });
	}
    
    $('#rolepermission_left_tree_1_check').attr("disabled","true");
   
    rolepermission.zrightNodes=[];
	rolepermission.rightsetting = {
		checkable: true,
		expandSpeed : "",
		checkType : { "Y": "", "N": "" },
		async: true
		//callback: {change:	zrightTreeOnChange}
	};

	//function zrightTreeOnChange(event, treeId, treeNode) {}

	function rightTree(asyncUrl) {
	    rolepermission.rightsetting.asyncUrl = asyncUrl;
		rolepermission.zrightTree = $("#rolepermission_right_tree").zTree(rolepermission.rightsetting, rolepermission.zrightNodes);
	}
	
    $(document).ready(function(){
        //rlefTree();
		//rightTree('system/RolePermission/getRolePermissionTree.do?sort='+rolepermission.sort+'&roleId='+$("#rolepermission_role_index").val());

		$.ajax({
            type: "POST",
            url:"system/Role/getRoleComboBox.do",
            dataType: "json",
            success:function(json){
                // $("#rolepermission_role_index").html('');
                // $("#rolepermission_role_index").append(json.msg);
                 rolepermission.zleftNodes = strToObj(json.msg);
                  rlefTree();
            }
        });
        
        $('#rolepermission_role_index').change(function(){
             //var selectedNode = rolepermission.zleftTree.getCheckedNodes(); 
             rightTree('system/RolePermission/getRolePermissionTree.do?sort=0'+'&roleId='+$("#rolepermission_role_index").val());
        });
	});
	
	function rolepermissionPage() {
	    $("#rolepermission_left_div").height(mDiv.height-112);
	    $("#rolepermission_right_div").height(mDiv.height-112);
	}

	$(window).resize(function() {
        setTimeout(function() {
	        rolepermissionPage();
        }, 500)
    });
    
    rolepermissionPage();
    
    setTimeout(function() {
	    rightTree('system/RolePermission/getRolePermissionTree.do?sort='+rolepermission.sort+'&roleId='+$("#rolepermission_role_index").val());
    }, 600)
    

</script>